<template>
    <div>
        <center><h3>修改密码</h3></center>
        <el-form ref="updateForm"  :model="doctor" :rules="rules"  label-width="80px" class="login-box">
            <el-form-item label="原密码"  class="out" prop="old" >
                <el-input v-model="doctor.oldpassword" type="password" class="inputs" placeholder="请输入原密码"  />
            </el-form-item>
            <el-form-item label="新密码"  class="out" prop="password" >
                <el-input v-model="doctor.password" type="password" class="inputs" placeholder="请输入新密码"  />
            </el-form-item>
            <el-form-item label="确认密码"  class="out" prop="passwords"  >
                <el-input v-model="doctor.passwords" type="password" class="inputs" placeholder="请再次输入密码"  />
            </el-form-item>
            <center>
                <el-form-item>
                    <el-button type="primary" @click="OnSubmit('updateForm')" >确认修改</el-button>
                </el-form-item>
            </center>
        </el-form>
    </div>
</template>

<script>
    import axios from  'axios'
    import {ElMessage} from "element-plus";
    export default {
        name: "DChangePassword",
        data(){
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    // if (this.user.passwords!== '') {
                    //     this.$refs.user.validateField('passwords');
                    // }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.doctor.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return{

                doctorId:'',
                secondList:{
                    id:'',
                    name:'',
                },
                doctor: {
                    avatar:'',
                    deleted:0,
                    gmtCreate:'',
                    gmtUpdate:'',
                    id: '',
                    name: '',
                    info:'',
                    career:'',
                    level:'',
                    sex:'',
                    idCard:'',
                    password:'',
                    phone:'',
                    oldpassword:'',
                    departmentId: '',
                    passwords:'',

                    old:'',
                },
                rules: {
                    password: [
                        {required:true,message:"密码不可为空",trigger:'blur'},
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    passwords: [
                        {required:true,message:"第二次密码不可为空",trigger:'blur'},
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    old: [
                        {required:true,message:"原密码不可为空",trigger:'blur'},
                    ],
                }

            };
        },
        created(){
            this.create();
        },
        methods: {
            create(){
                this.doctorId=localStorage.getItem("id");
                axios.get("http://119.29.60.153:8001/doctorservice/doctor/GetDoctorById/"+this.doctorId).then(resp=> {
                    this.doctor=resp.data.data.doctor;
                    this.doctor.old=this.doctor.password;
                    this.doctor.password='';
                })
            },
            OnSubmit(formName){
                this.$refs[formName].validate((valid)=>{
                    if(valid){
                        if(this.doctor.old!=this.doctor.oldpassword)
                        {
                            ElMessage.error('原密码错误');
                            return ;
                        }
                        else {
                            axios.post("http://119.29.60.153:8001/doctorservice/doctor/updateDoctor",this.doctor).then(resp=> {

                                if(resp.data.success==true){
                                    alert("修改成功!");
                                    location.reload();
                                    this.$router.push("/dashboard")
                                }
                                else {
                                    ElMessage.success("添加失败!");
                                    this.addFormVisible=false;

                                }
                            })
                        }


                    }else {
                        return false;
                    }
                })

            },
        }
    }
</script>

<style scoped>
    .login-box{

        padding: 10px 35px 15px 35px;

    }
    .out{
        margin-left: 350px;
    }
    .inputs{
        width: 500px;
    }
    .avatar-uploader {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        width: 100px;
        height: 100px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 100px;
        height: 100px;
        line-height: 100px;
        text-align: center;
    }
    .avatar {
        width: 100px;
        height: 100px;
        display: block;
    }

</style>